package com.esri.android.map;

import android.util.Log;
import com.esri.android.map.ags.ArcGISFeatureLayer;
import com.esri.android.map.event.OnStatusChangedListener;
import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.GeometryEngine;
import com.esri.core.geometry.Point;
import com.esri.core.geometry.SpatialReference;
import com.esri.core.io.EsriSecurityException;
import com.esri.core.map.FeatureSet;
import com.esri.core.map.Graphic;
import com.esri.core.symbol.Symbol;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes.dex */
public class CSVLayer extends ArcGISFeatureLayer {
    private final Envelope Q;
    File a;
    final CSVConfig b;
    boolean c;

    /* loaded from: classes.dex */
    public static class CSVConfig {
        public String url = null;
        public String latitudeField = null;
        public String longitudeField = null;
        public String columnDelimiter = null;
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes.dex */
    public class a {
        int a;

        a(int i) {
            this.a = -1;
            this.a = i;
        }

        int a() {
            return this.a;
        }

        void a(List<Point> list, List<Map<String, Object>> list2) {
            if (list.isEmpty()) {
                return;
            }
            SpatialReference spatialReference = CSVLayer.this.getSpatialReference();
            ArrayList arrayList = new ArrayList();
            for (int i = 0; i < list.size(); i++) {
                Point point = list.get(i);
                Point project = GeometryEngine.project(point.getX(), point.getY(), spatialReference);
                CSVLayer.this.Q.merge(project);
                if (project.isEmpty()) {
                    Log.i(com.esri.core.internal.a.a, "The location of the CSV record can not be projected into map's SR.");
                } else {
                    arrayList.add(new Graphic(project, (Symbol) null, list2.get(i)));
                }
            }
            CSVLayer.this.addGraphics((Graphic[]) arrayList.toArray(new Graphic[0]));
        }
    }

    public CSVLayer(String str, CSVConfig cSVConfig, boolean z) {
        super(str, (String) null, (FeatureSet) null, (ArcGISFeatureLayer.Options) null, false);
        CSVConfig cSVConfig2 = new CSVConfig();
        this.b = cSVConfig2;
        this.c = false;
        this.Q = new Envelope();
        if (cSVConfig != null) {
            cSVConfig2.url = cSVConfig.url;
            cSVConfig2.latitudeField = cSVConfig.latitudeField;
            cSVConfig2.longitudeField = cSVConfig.longitudeField;
            cSVConfig2.columnDelimiter = cSVConfig.columnDelimiter;
            if (z) {
                b();
                return;
            }
            return;
        }
        Log.e(com.esri.core.internal.a.a, "Failed to initialize the ArcGISFeatureLayer. CSVConfig instance is null");
        changeStatus(OnStatusChangedListener.STATUS.fromInt(OnStatusChangedListener.EsriStatusException.INIT_FAILED_ARCGIS_FEATURE_LAYER));
    }

    @Override // com.esri.android.map.Layer
    public Envelope getFullExtent() {
        Envelope fullExtent = super.getFullExtent();
        if (fullExtent != null && !this.Q.contains(fullExtent)) {
            this.Q.merge(fullExtent);
        }
        return this.Q;
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // com.esri.android.map.ags.ArcGISFeatureLayer, com.esri.android.map.GraphicsLayer, com.esri.android.map.Layer
    public void initLayer() {
        try {
        } catch (InterruptedException unused) {
            Log.i(com.esri.core.internal.a.a, "The operation is cancelled.");
        } catch (Exception e) {
            Log.e(com.esri.core.internal.a.a, "Failed to initialize the CSV Layer.", e);
            if (e instanceof EsriSecurityException) {
                changeStatus(OnStatusChangedListener.STATUS.fromInt(((EsriSecurityException) e).getCode()));
            } else {
                changeStatus(OnStatusChangedListener.STATUS.fromInt(OnStatusChangedListener.EsriStatusException.INIT_FAILED_ARCGIS_FEATURE_LAYER));
            }
        }
        if (this.b.url == null || this.b.url.trim().equals("") || this.b.latitudeField == null || this.b.latitudeField.trim().equals("") || this.b.longitudeField == null || this.b.longitudeField.trim().equals("") || this.b.columnDelimiter == null || (!this.b.columnDelimiter.equals("\t") && this.b.columnDelimiter.trim().equals(""))) {
            Log.w(com.esri.core.internal.a.a, "Some property in CSVConfig object is missing! The CSV records will not be rendered!");
            throw new IllegalArgumentException("CSV Configuration parameter is mssing.");
        }
        URI uri = new URI(this.b.url);
        String scheme = uri.getScheme();
        setDefaultSpatialReference(SpatialReference.create(SpatialReference.WKID_WGS84));
        if (scheme.equals("file")) {
            this.a = new File(uri);
            this.c = true;
        } else {
            if (!scheme.equals("http") && !scheme.equals("https")) {
                throw new UnsupportedOperationException("The CSV file scheme " + scheme + " is not supported!");
            }
            b(this.b.url);
            this.c = false;
        }
        super.initLayer();
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // com.esri.android.map.ags.ArcGISFeatureLayer
    public void initialUpdate() {
        super.initialUpdate();
        getPoolExecutor().submit(new Runnable() { // from class: com.esri.android.map.CSVLayer.1
            @Override // java.lang.Runnable
            public void run() {
                try {
                    CSVLayer cSVLayer = CSVLayer.this;
                    cSVLayer.a(cSVLayer.a, CSVLayer.this.getFields(), CSVLayer.this.b.latitudeField, CSVLayer.this.b.longitudeField, CSVLayer.this.b.columnDelimiter, new a(1000), !CSVLayer.this.c);
                } catch (Exception e) {
                    Log.w(com.esri.core.internal.a.a, "Failed to parse the CSV file.", e);
                }
            }
        });
    }

    private void b(String str) throws Exception {
        FileOutputStream fileOutputStream;
        if (str == null) {
            return;
        }
        InputStream inputStream = null;
        try {
            this.a = File.createTempFile("csv", null);
            fileOutputStream = new FileOutputStream(this.a);
            try {
                inputStream = com.esri.core.internal.io.handler.c.a(str, new ArrayList(), (String) null).getEntity().getContent();
                byte[] bArr = new byte[4096];
                while (true) {
                    int read = inputStream.read(bArr);
                    if (read < 0) {
                        break;
                    }
                    fileOutputStream.write(bArr, 0, read);
                }
                fileOutputStream.flush();
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                        Log.w(com.esri.core.internal.a.a, "Failed to close input stream", e);
                    }
                }
                try {
                    fileOutputStream.close();
                } catch (IOException e2) {
                    Log.w(com.esri.core.internal.a.a, "Failed to close output stream", e2);
                }
            } catch (Throwable th) {
                th = th;
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e3) {
                        Log.w(com.esri.core.internal.a.a, "Failed to close input stream", e3);
                    }
                }
                if (fileOutputStream != null) {
                    try {
                        fileOutputStream.close();
                    } catch (IOException e4) {
                        Log.w(com.esri.core.internal.a.a, "Failed to close output stream", e4);
                    }
                }
                throw th;
            }
        } catch (Throwable th2) {
            th = th2;
            fileOutputStream = null;
        }
    }

    /* JADX WARN: Code restructure failed: missing block: B:82:0x019a, code lost:
        if (r32 != false) goto L87;
     */
    /* JADX WARN: Code restructure failed: missing block: B:83:0x019c, code lost:
        r26.delete();
     */
    /* JADX WARN: Code restructure failed: missing block: B:96:0x01b7, code lost:
        if (r32 != false) goto L87;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct code enable 'Show inconsistent code' option in preferences
    */
    void a(java.io.File r26, com.esri.core.map.Field[] r27, java.lang.String r28, java.lang.String r29, java.lang.String r30, com.esri.android.map.CSVLayer.a r31, boolean r32) {
        /*
            Method dump skipped, instructions count: 465
            To view this dump change 'Code comments level' option to 'DEBUG'
        */
        throw new UnsupportedOperationException("Method not decompiled: com.esri.android.map.CSVLayer.a(java.io.File, com.esri.core.map.Field[], java.lang.String, java.lang.String, java.lang.String, com.esri.android.map.CSVLayer$a, boolean):void");
    }

    private String[] a(String str, Pattern pattern) {
        String group;
        Matcher matcher = pattern.matcher(str);
        ArrayList arrayList = new ArrayList();
        while (matcher.find()) {
            if (matcher.group(1) != null) {
                group = matcher.group(1);
            } else if (matcher.group(3) != null) {
                group = matcher.group(3);
            } else {
                group = matcher.group(2);
            }
            if (group.contains("\"\"")) {
                group = group.replace("\"\"", "\"");
            }
            arrayList.add(group);
        }
        return (String[]) arrayList.toArray(new String[1]);
    }
}
